﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using RestaurantMIS.Model;

namespace RestaurantMIS
{
    public partial class frmPayOrderPrint : Form
    {
        #region Members

        private int payOrderId;
        private string _printHtml = string.Empty;
        private string _FilledContent = string.Empty;
        public BLL.PayOrder bllpayorder = new RestaurantMIS.BLL.PayOrder();
        public BLL.PayOrderItem bllpayorderitem = new RestaurantMIS.BLL.PayOrderItem();

        #endregion

        public frmPayOrderPrint()
        {
            InitializeComponent();
        }

        public frmPayOrderPrint(int OrderId)
        {
            payOrderId = OrderId;
            InitializeComponent();
        }

        private void frmPayOrderPrint_Load(object sender, EventArgs e)
        {
            ConstructHtmlString();
            wbReportView.DocumentText = _printHtml;
        }

        private void ConstructHtmlString()
        {
            var payorder = bllpayorder.GetModel(payOrderId);
            var payorderitems = bllpayorderitem.GetModelList("PayOrderID=" + payOrderId);

            #region Build HTML

            StringBuilder sbHtml = new StringBuilder();
            sbHtml.AppendLine("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">");
            sbHtml.AppendLine("<html>");
            sbHtml.AppendLine("<head>");
            sbHtml.AppendLine("    <title></title>");
            sbHtml.AppendLine("    <style type=\"text/css\">");
            sbHtml.AppendLine("        table");
            sbHtml.AppendLine("        {");
            sbHtml.AppendLine("            border-collapse: collapse;");
            sbHtml.AppendLine("            width: 160mm;");
            sbHtml.AppendLine("        }");
            sbHtml.AppendLine("        table, th, td");
            sbHtml.AppendLine("        {");
            sbHtml.AppendLine("            border: 1px solid black;");
            sbHtml.AppendLine("        }");
            sbHtml.AppendLine("        td");
            sbHtml.AppendLine("        {");
            sbHtml.AppendLine("            width: 50%;");
            sbHtml.AppendLine("            padding: 8;");
            sbHtml.AppendLine("        }");
            sbHtml.AppendLine("        .style1");
            sbHtml.AppendLine("        {");
            sbHtml.AppendLine("            font-weight: bold;");
            sbHtml.AppendLine("            font-size:16px;");
            sbHtml.AppendLine("        }");
            sbHtml.AppendLine("        .style2");
            sbHtml.AppendLine("        {");
            sbHtml.AppendLine("            font-size:14px;");
            sbHtml.AppendLine("        }");
            sbHtml.AppendLine("    </style>");
            sbHtml.AppendLine("</head>");
            sbHtml.AppendLine("<body>");
            sbHtml.AppendLine("    <table>");
            sbHtml.AppendLine("        <tr class=\"style1\">");
            sbHtml.AppendLine("            <td>");
            sbHtml.AppendLine("                台号：" + payorder.RoomID.ToString() + "");
            sbHtml.AppendLine("            </td>");
            sbHtml.AppendLine("            <td>");
            sbHtml.AppendLine("                日期：" + payorder.LogDate.ToShortDateString() + "&nbsp;&nbsp;&nbsp;" + payorder.Category);
            sbHtml.AppendLine("            </td>");
            sbHtml.AppendLine("        </tr>");
            sbHtml.AppendLine("        <tr class=\"style1\">");
            sbHtml.AppendLine("            <td colspan=\"2\">");
            sbHtml.AppendLine("                点菜总计：￥" + payorder.FoodSum.ToString("0.00") + " 元");
            sbHtml.AppendLine("            </td>");
            sbHtml.AppendLine("        </tr>");
            sbHtml.AppendLine(BuildItems(payorderitems, "点菜"));
            sbHtml.AppendLine("        <tr class=\"style1\">");
            sbHtml.AppendLine("            <td colspan=\"2\">");
            sbHtml.AppendLine("                酒水总计：￥" + payorder.WineSum.ToString("0.00") + " 元");
            sbHtml.AppendLine("            </td>");
            sbHtml.AppendLine("        </tr>");
            sbHtml.AppendLine(BuildItems(payorderitems, "酒水"));
            sbHtml.AppendLine("        <tr class=\"style1\">");
            sbHtml.AppendLine("            <td colspan=\"2\">");
            sbHtml.AppendLine("                烟类总计：￥" + payorder.CigaretteSum.ToString("0.00") + " 元");
            sbHtml.AppendLine("            </td>");
            sbHtml.AppendLine("        </tr>");
            sbHtml.AppendLine(BuildItems(payorderitems, "烟类"));
            sbHtml.AppendLine("        <tr class=\"style1\">");
            sbHtml.AppendLine("            <td>");
            sbHtml.AppendLine("                其它总计：￥" + payorder.OtherSum.ToString("0.00") + " 元");
            sbHtml.AppendLine("            </td>");
            sbHtml.AppendLine("            <td>");
            sbHtml.AppendLine("                应收金额：￥" + payorder.TotalSum.ToString("0.00") + " 元");
            sbHtml.AppendLine("            </td>");
            sbHtml.AppendLine("        </tr>");
            sbHtml.AppendLine("        <tr class=\"style1\">");
            sbHtml.AppendLine("            <td>");
            sbHtml.AppendLine("            </td>");
            sbHtml.AppendLine("            <td>");
            sbHtml.AppendLine("                实收金额：￥" + payorder.RealSum.ToString("0.00") + " 元");
            sbHtml.AppendLine("            </td>");
            sbHtml.AppendLine("        </tr>");
            sbHtml.AppendLine("    </table>");
            sbHtml.AppendLine("</body>");
            sbHtml.AppendLine("</html>");
            _printHtml = sbHtml.ToString();

            #endregion
        }

        private string BuildItems(List<PayOrderItem> list, string categoryName)
        {
            StringBuilder sb = new StringBuilder();

            List<PayOrderItem> items = list.Where(i => i.Category == categoryName).ToList();

            for (int i = 0; i < items.Count; i += 2)
            {
                sb.AppendLine("<tr class=\"style2\">");
                sb.AppendLine("<td>");
                sb.AppendLine(items[i].DisplayName);
                sb.AppendLine("</td>");
                if ((i + 1) < items.Count)
                {
                    sb.AppendLine("<td>");
                    sb.AppendLine(items[i + 1].DisplayName);
                    sb.AppendLine("</td>");
                }
                sb.AppendLine("</tr>");
            }

            return sb.ToString();
        }

        private string StringPrint(string htmltable, int key)
        {
            StringBuilder sb = new StringBuilder();
            string Htmls = "";
            sb.Append(htmltable);
            switch (key)
            {
                case 1://打印  
                    sb.Append("<script languge='javascript'>window.print(); window.opener=null;</script>"); break;
                case 2://直接打印  
                    sb.Append("<script languge='javascript'>WebBrowser.ExecWB(6,6); window.opener=null;</script>"); break;
                case 3://打印预览  
                    sb.Append("<script languge='javascript'>WebBrowser.ExecWB(7,1); window.opener=null;</script>"); break;
                case 4://打印设置  
                    sb.Append("<script languge='javascript'>WebBrowser.ExecWB(8,1); window.opener=null;</script>"); break;
                default: //打印  
                    sb.Append("<script languge='javascript'>WebBrowser.ExecWB(6,1); window.opener=null;</script>"); break;
            }
            Htmls = sb.ToString();
            return (Htmls);
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            wbReportView.DocumentText = StringPrint(_printHtml, 1);
        }
    }
}
